﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Hisun.Models;
using Hisun.Controllers;

namespace Hisun.View.DataExchange
{
    public partial class VoucherCheck : System.Web.UI.Page
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AbcFullAccountPeriod MyFullAp = new AbcFullAccountPeriod();

                MyFullAp.FullAccountPeriodToDropdownlist(DropDownList_Year, DropDownList_Month, Application["accountPeriod"].ToString());
                //string m = DateTime.Now.Month.ToString();
                string m = Application["accountPeriod"].ToString().Substring(4,2);
                //if (Convert.ToInt16(m) < 9)
                //{
                //    m = "0" + m;
                //} ;
                DropDownList_Month.SelectedValue = m;

                int Totals = TotalPageAmount();
                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1页，转到";
                BindMyData(0);

            }
        }


        private void BindMyData(int PageIndex)
        {

            int skipAccount = PageIndex * 10;

            string AccountPeriod = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); 

            List<VABC_NC_VoucherSum> Query1 = new List<VABC_NC_VoucherSum>();
            List<VABC_NC_VoucherSum> Query2 = new List<VABC_NC_VoucherSum>();

             var IsValidString = from a in db.TABC_JS_AccountPeriods
                                where a.会计期间ID == AccountPeriod
                                select a;
            if (IsValidString.Count() == 0)
            {
                Response.Write("<Script>alert('会计期间不存在，请检查输入是否正确')</Script>");
                return;
            }
            else if (IsValidString.Count() == 1)
            {
                switch (DropDownList_Field.SelectedIndex)
                {
                    case 0:
                        Query1 = (from a in db.VABC_NC_VoucherSum where a.部门编码.Contains(TextBox_QueryValue.Text) select a).ToList();

                        break;
                    case 1:

                        Query1 = (from a in db.VABC_NC_VoucherSum where a.部门名称.Contains(TextBox_QueryValue.Text) select a).ToList();

                        break;

                 

                }

                Query2 = (from a in Query1
                          where a.会计期间ID == AccountPeriod
                          select a).ToList();
            }
           
            var MyQuery = (from a in Query2
                           select a).ToList().Skip(skipAccount).Take(10);
            GridView_1.DataSource = MyQuery;
            GridView_1.DataBind();

            if (GridView_1.Rows.Count == 0)
            { Panel_Main.Visible = false; }
            else
            { Panel_Main.Visible = true; }

        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;
            string AccountPeriod = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); 

            List<VABC_NC_VoucherSum> Query1 = new List<VABC_NC_VoucherSum>();
            List<VABC_NC_VoucherSum> Query2 = new List<VABC_NC_VoucherSum>();

            switch (DropDownList_Field.SelectedIndex)
            {
                case 0:
                    Query1 = (from a in db.VABC_NC_VoucherSum where a.部门编码.Contains(TextBox_QueryValue.Text) select a).ToList();

                    break;
                case 1:

                    Query1 = (from a in db.VABC_NC_VoucherSum where a.部门名称.Contains(TextBox_QueryValue.Text) select a).ToList();

                    break;



            }

            Query2 = (from a in Query1
                      where a.会计期间ID == AccountPeriod
                      select a).ToList();

            
            var Query4 = (from a in Query2
                          select a).ToList();
            MyQuery = Query4.Count();
         
            return MyQuery;
        }

        #region 实现翻页功能的代码段
        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {

                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                    case "":
                        pageIndx = 0;
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);

        }
        #endregion


    }
}
